﻿using Ease.EaseDialog.Runtime.Data;
using Ease.EaseNode.Editor.NodeView;
using Ease.EaseNode.Runtime;
using Ease.EaseNode.Runtime.Param;
using Ease.TestEaseNode.Runtime.Param;
using UnityEngine;
using UnityEngine.UIElements;
using StringChangeParam = Ease.EaseDialog.Runtime.Param.StringChangeParam;

namespace Ease.EaseDialog.Scripts.Editor.Node
{
    public class DialogNode2 : BaseEaseNode
    {
        private DialogData2 dialogData;
        private TextField textFieldName;
        private TextField textFieldContent;

        public DialogNode2(IEaseNodeDataState state) : base(state)
        {
            title = State == null ? "TestEaseNode" : $"TestEaseNode_{state.Key}";
            contentContainer.style.backgroundColor = Color.gray;
            textFieldName = new TextField();
            textFieldName.RegisterCallback<ChangeEvent<string>>(OnTextFieldNameChange);
            textFieldContent = new TextField();
            textFieldContent.multiline = true;
            textFieldContent.RegisterCallback<ChangeEvent<string>>(OnTextFieldCotentChange);
            contentContainer.Add(textFieldName);
            contentContainer.Add(textFieldContent);
            if (State != null)
            {
                dialogData = State.Data as DialogData2;
            }

            if (dialogData != null)
            {
                title = dialogData.Tip;
                textFieldName.SetValueWithoutNotify(dialogData.name);
                textFieldContent.SetValueWithoutNotify(dialogData.content);
            }

            if (State != null)
            {
                switch (State.CurrentNodeState)
                {
                    case NodeState.None:
                        contentContainer.style.backgroundColor = Color.gray;
                        break;
                    case NodeState.Start:
                        contentContainer.style.backgroundColor = Color.green;
                        break;
                    case NodeState.Running:
                        contentContainer.style.backgroundColor = Color.red;
                        break;
                    case NodeState.Finish:
                        contentContainer.style.backgroundColor = Color.blue;
                        break;
                    default:
                        break;
                }
            }
        }

        protected override void OnStateChange(BaseChangeParam param)
        {
            if (param is StringChangeParam a)
            {
                if (a.valueKey == "name")
                    textFieldName.SetValueWithoutNotify(a.value);
                else if (a.valueKey == "content")
                    textFieldContent.SetValueWithoutNotify(a.value);
            }
            else if (param is StateChangeParam c)
            {
                switch (c.newState)
                {
                    case NodeState.None:
                        contentContainer.style.backgroundColor = Color.gray;
                        break;
                    case NodeState.Start:
                        contentContainer.style.backgroundColor = Color.green;
                        break;
                    case NodeState.Running:
                        contentContainer.style.backgroundColor = Color.red;
                        break;
                    case NodeState.Finish:
                        contentContainer.style.backgroundColor = Color.blue;
                        break;
                    default:
                        break;
                }
            }
        }

        private void OnTextFieldNameChange(ChangeEvent<string> a)
        {
            dialogData.name = a.newValue;
        }

        private void OnTextFieldCotentChange(ChangeEvent<string> a)
        {
            dialogData.content = a.newValue;
        }
    }
}